package com.example.home.bus.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.home.R;
import com.example.home.bus.adapter.BusListAdapter;
import com.example.home.bus.bean.BusListBean;
import com.example.home.tool.OKHttpTool;


public class BusFragment extends Fragment {


    private View view;
    private RecyclerView rcvList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_bus, container, false);


        getBusList();

        return view;





    }

    private void getBusList() {
        OKHttpTool.doNetRequest("prod-api/api/bus/line/list?pageNum=1&pageSize=8",
                OKHttpTool.GET,
                BusListBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        BusListBean busListBean = (BusListBean) obj;
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindList(busListBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void bindList(BusListBean busListBean) {
        RecyclerView recyclerView = view.findViewById(R.id.rcv_list);
        BusListAdapter busListAdapter = new BusListAdapter(getContext(),busListBean.getRows());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        recyclerView.setAdapter(busListAdapter);
        recyclerView.setLayoutManager(linearLayoutManager);
    }

}